package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.QuizRecord;
import com.jyy.prefabricated.entity.Student;
import com.jyy.prefabricated.service.QuizRecordService;
import com.jyy.prefabricated.utils.UserUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 学生随堂考记录表(QuizRecord)表控制层
 *
 * @author makejava
 * @since 2021-10-11 14:05:47
 */
@RestController
@RequestMapping("/api")
public class QuizRecordController {
    /**
     * 服务对象
     */
    @Resource
    private QuizRecordService quizRecordService;

    @Resource
    private UserUtils userUtils;


    /**
     * 获取用户随堂考记录
     *
     * @param coursePackageId 学生随堂考id
     * @return
     */
    @GetMapping("/quizRecord")
    public AjaxResult selectByCoursePackageId(Pageable pageable, @RequestParam(required = false) Integer coursePackageId) {
        Student student = userUtils.getCurrentStudent();
        QuizRecord quizRecord = new QuizRecord();
        quizRecord.setStudentId(student.getId());
        quizRecord.setCoursePackageId(coursePackageId);
        List<QuizRecord> result = quizRecordService.selectByParams(quizRecord);

        return AjaxResult.success(result);
    }

    /**
     * 获取用户随堂考记录
     *
     * @param studentId 学生随堂考id
     * @param orderId   订单id
     * @param pageable
     */
    @GetMapping("/quizRecordList")
    public AjaxResult selectQuizRecordByOrderId(Pageable pageable, @RequestParam(required = false) Integer orderId, @RequestParam(required = false) Integer studentId) {
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        QuizRecord quizRecord = new QuizRecord();
        quizRecord.setStudentId(studentId);
        quizRecord.setOrderId(orderId);
        List<QuizRecord> quizRecordList = quizRecordService.selectByStudentIdAndOrderId(quizRecord);
        PageInfo<QuizRecord> result = new PageInfo<>(quizRecordList);
        return AjaxResult.success(result);
    }
}
